//
//  HJHUD.m
//  KaiChat
//
//  Created by lx on 2023/10/18.
//

#import "HJHUD.h"
#import <MBProgressHUD.h>
#import "HJLoadingView.h"

static const NSInteger showMessageTag = 100111;
static const NSInteger loadingViewTag = 100112;

@implementation HJHUD


+ (void)showLoading {
    [self showLoadingInView:[UIApplication sharedApplication].delegate.window];
}

+ (void)showLoadingInView:(nullable UIView *)view {
    [self showLoadingInView:view text:nil];
}

+ (void)showGraceTimeLoadingInView:(nullable UIView *)view {
    [self showLoadingInView:view text:nil graceTime:loadGraceTime];
}

+ (void)showLoadingInView:(nullable UIView *)view text:(nullable NSString *)text {
    [self showLoadingInView:view text:text graceTime:0];
}

+ (void)showLoadingInView:(nullable UIView *)view text:(nullable NSString *)text graceTime:(NSTimeInterval)graceTime {
    
    [HJTools.tool.currentHud hideAnimated:NO];
    [MBProgressHUD hideHUDForView:view animated:YES];
    
    HJLoadingView *activityIndicatorView = [[HJLoadingView alloc] init];
    activityIndicatorView.hidesWhenStopped = NO;
    [activityIndicatorView startAnimating];
    
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
    hud.removeFromSuperViewOnHide = YES;
    if (graceTime > 0) {
        hud.graceTime = graceTime;
    }
    [view addSubview:hud];
    [hud showAnimated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    
    hud.bezelView.layer.cornerRadius = 10;
    hud.bezelView.layer.masksToBounds = YES;
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.alpha = 1;
    hud.margin = 10;
    hud.opaque = 1;
    hud.customView = activityIndicatorView;
    hud.detailsLabel.text = text;
    hud.detailsLabel.font = UIFontMake(14);
    hud.detailsLabel.textColor = UIColor.whiteColor;
    hud.bezelView.backgroundColor = [UIColor.tj_333333Color colorWithAlphaComponent:0.9];

    if (text) {
        CGFloat totalH = hud.margin * 2 + 6 + activityIndicatorView.intrinsicContentSize.height + [text getStringSizeWithWidth:300 font:hud.detailsLabel.font].height;
        hud.minSize = CGSizeMake(totalH, totalH);
    }
    
    hud.tag = loadingViewTag;
    hud.removeFromSuperViewOnHide = YES;
    HJTools.tool.currentHud = hud;
}

+ (void)updateHudText:(NSString *)text {
    HJTools.tool.currentHud.detailsLabel.text = text;
}
 
+ (void)hiddenLoading {
    dispatch_safe_queue(^{
        MBProgressHUD *hud = HJTools.tool.currentHud;
        HJLoadingView *loadingView = (HJLoadingView *)hud.customView;
        if ([loadingView isKindOfClass:HJLoadingView.class]) {
            [loadingView stopAnimating];
        }
        [hud hideAnimated:NO];
        HJTools.tool.currentHud = nil;
    });
}

+ (void)hiddenLoadingInView:(nullable UIView *)view {
    dispatch_safe_queue(^{
        [HJTools.tool.currentHud hideAnimated:NO];
        MBProgressHUD *hud = [MBProgressHUD HUDForView:view];
        HJLoadingView *loadingView = (HJLoadingView *)hud.customView;
        [loadingView stopAnimating];
        [hud removeFromSuperview];
        [MBProgressHUD hideHUDForView:view animated:YES];
        HJTools.tool.currentHud = nil;
    });
}

+ (MBProgressHUD *)getShowMessageHud {
    return [getMainWindow() viewWithTag:showMessageTag];
}

+ (void)hiddenMessage {
    dispatch_safe_queue(^{
        [HJTools.tool.currentHud hideAnimated:NO];
        MBProgressHUD *messageHud = [getMainWindow() viewWithTag:showMessageTag];
        HJLoadingView *loadingView = (HJLoadingView *)messageHud.customView;
        if ([loadingView isKindOfClass:HJLoadingView.class]) {
            [loadingView stopAnimating];
        }
        [messageHud hideAnimated:NO];
        HJTools.tool.currentHud = nil;
    });
}


+ (void)showMessage:(nullable NSString *)message {
    [self showMessage:message inView:getMainWindow() offsetY:0 type:HJHUDTypeNone];
}

+ (void)showMessage:(nullable NSString *)message offsetY:(CGFloat)offsetY {
    [self showMessage:message inView:[UIApplication sharedApplication].delegate.window offsetY:offsetY type:HJHUDTypeNone];
}

+ (void)showMessage:(nullable NSString *)message inView:(UIView *)view {
    [self showMessage:message inView:view offsetY:0 type:HJHUDTypeNone];
}

+ (void)showErrorMessage:(nullable NSString *)message inView:(UIView *)view {
    [self showMessage:message inView:view offsetY:0 type:HJHUDTypeError];
}

+ (void)showErrorMessage:(nullable NSString *)message {
    [self showMessage:message inView:getMainWindow() offsetY:0 type:HJHUDTypeError];
}

+ (void)showSucceedMessage:(nullable NSString *)message inView:(UIView *)view {
    [self showMessage:message inView:view offsetY:0 type:HJHUDTypeSucceed];
}

+ (void)showSucceedMessage:(nullable NSString *)message {
    [self showMessage:message inView:getMainWindow() offsetY:0 type:HJHUDTypeSucceed];
}

+ (void)showSucceedMessageTip {
    [self showSucceedMessage:@"Operating succeeded"];
}

+ (void)showErrorMessageTip {
    [self showErrorMessage:@"Operating failed"];
}
 
+ (void)showMessage:(nullable NSString *)message inView:(UIView *)view offsetY:(CGFloat)offsetY type:(HJHUDType)type {

    dispatch_safe_queue(^{
        
        [HJTools.tool.currentHud hideAnimated:NO];
        [MBProgressHUD hideHUDForView:view animated:YES];
            
        if (isEmptyString(message)) return;
        
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
        hud.bezelView.backgroundColor = [UIColor.tj_333333Color colorWithAlphaComponent:0.9];
        hud.bezelView.layer.cornerRadius = 4;
        hud.bezelView.layer.masksToBounds = YES;
        
        if (type == HJHUDTypeNone) {
            hud.detailsLabel.text = message;
        } else {
            NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@" %@", message]];
            NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
            textAttachment.image = [UIImage imageNamed: type == HJHUDTypeSucceed ? @"success_icon" : @"error_icon"];
            textAttachment.bounds = CGRectMake(0, -4, textAttachment.image.size.width, textAttachment.image.size.height);
            NSAttributedString *iconAtt = [NSAttributedString attributedStringWithAttachment:textAttachment];
            [att insertAttributedString:iconAtt atIndex:0];
            hud.detailsLabel.attributedText = att;
        }

        hud.tag = showMessageTag;
        hud.detailsLabel.font = UIFontMake(14);
        hud.detailsLabel.numberOfLines = 10;
        hud.detailsLabel.textColor = UIColor.whiteColor;
        hud.margin = 10;
        hud.removeFromSuperViewOnHide = YES;
        CGPoint offset = hud.offset;
        offset.y = offsetY;
        hud.offset = offset;
        [hud hideAnimated:YES afterDelay:2];
        hud.userInteractionEnabled = NO;
        HJTools.tool.currentHud = hud;
    });
}

+ (void)showLogMessage:(nullable NSString *)message {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].delegate.window animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = message;
    hud.label.font = UIFontMediumMake(15);
    hud.margin = 10;
    hud.removeFromSuperViewOnHide = YES;
    CGPoint offset = hud.offset;
    offset.y = KScreenHeight * 0.5 - 200;
    hud.offset = offset;
}



@end
